package hivi.vn.hivigold.activities;

import hivi.vn.hivigold.R;
import hivi.vn.hivigold.adapters.HistoryAdapter;
import hivi.vn.hivigold.database.WebService;
import hivi.vn.hivigold.objects.Employee;
import hivi.vn.hivigold.objects.HistoryDetail;
import hivi.vn.hivigold.objects.HistoryMaster;
import hivi.vn.hivigold.objects.Partner;
import hivi.vn.hivigold.objects.ReportFilter;
import hivi.vn.hivigold.objects.Stock;
import hivi.vn.hivigold.utils.Formats;

import java.util.ArrayList;
import java.util.Date;

import org.kobjects.util.Util;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

public class PartnerHistory extends Activity {
	public static String PARTNERSELECTED = "PARTNERSELECTED";
	TextView txtPartner;
	TextView txtDfrom;
	TextView txtDto;
	TextView txtVtID;
	TextView txtNvID;
	TextView txtKhoID;
	TextView txtNhID;
	ExpandableListView lstReport;
	Button btnExport;
	EditText txtTotal;

	int mYear;
	int mMonth;
	int mDay;
	Date dFrom;
	Date dTo;
	int flagDate = -1;
	static final int DFROM = 1;
	static final int DTO = 2;
	Partner partner;
	boolean flagFirst = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.history_layout);
		btnExport = (Button) findViewById(R.id.btnExport);
		txtDfrom = (EditText) findViewById(R.id.txtDFrom);
		txtDto = (EditText) findViewById(R.id.txtDTo);
		txtVtID = (EditText) findViewById(R.id.txtVtID);
		txtNhID = (EditText) findViewById(R.id.txtNhomID);
		txtNvID = (EditText) findViewById(R.id.txtNvID);
		txtKhoID = (EditText) findViewById(R.id.txtKhoID);
		txtPartner = (TextView) findViewById(R.id.txtHistory_partnerid);
		lstReport = (ExpandableListView) findViewById(R.id.lst_history);
		txtTotal = (EditText) findViewById(R.id.txtTotalHistory);
		partner =(Partner)getIntent().getExtras().getSerializable(PARTNERSELECTED);
		
		txtPartner.setText(partner.getPartnerID()+"-"+partner.getPartnerName());
		
		dFrom = Formats.StartDate();
		dTo = Formats.CurrentDate();
		txtDfrom.setText(Formats.DateFormatDisplay(dFrom));
		txtDto.setText(Formats.DateFormatDisplay(dTo));
		
		btnExport.setOnClickListener(onClick);
		txtDfrom.setOnClickListener(onClick);
		txtDto.setOnClickListener(onClick);
		txtKhoID.setOnLongClickListener(onLong);
		txtNvID.setOnLongClickListener(onLong);
		
		export();
	}

	DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			// TODO Auto-generated method stub
			if (flagDate == DFROM) {
				dFrom = Formats.createDate(year, monthOfYear, dayOfMonth);
				txtDfrom.setText(Formats.DateFormatDisplay(dFrom));
			} else if (flagDate == DTO) {
				dTo = Formats.createDate(year, monthOfYear, dayOfMonth);
				txtDto.setText(Formats.DateFormatDisplay(dTo));
			}
			flagDate = -1;
		}
	};

	OnClickListener onClick = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			if (arg0.getId() == R.id.txtDFrom) {
				flagDate = DFROM;
				DatePickerDialog mDatePickerDialog = new DatePickerDialog(
						PartnerHistory.this, mDateSetListener,
						Formats.getComDateNow(dFrom, "yyyy"),
						(Formats.getComDateNow(dFrom, "MM") - 1),
						Formats.getComDateNow(dFrom, "dd"));
				mDatePickerDialog.setTitle(R.string.dfrom);
				mDatePickerDialog.show();
			} else if (arg0.getId() == R.id.txtDTo) {
				flagDate = DTO;
				DatePickerDialog mDatePickerDialog = new DatePickerDialog(
						PartnerHistory.this, mDateSetListener,
						Formats.getComDateNow(dTo, "yyyy"),
						(Formats.getComDateNow(dTo, "MM") - 1),
						Formats.getComDateNow(dTo, "dd"));
				mDatePickerDialog.setTitle(R.string.dto);
				mDatePickerDialog.show();
			} else if (arg0.getId() == R.id.btnExport) {
				export();
			}

		}
	};

	private void export()
	{
		if (txtDfrom.getText().toString().trim().length() == 0
				|| txtDto.getText().toString().trim().length() == 0) {
			Toast.makeText(getBaseContext(), R.string.warning_notempty,
					Toast.LENGTH_LONG).show();
			return;
		}
		ReportFilter filter = new ReportFilter();
		filter.setDfrom(dFrom);
		filter.setDto(dTo);
		filter.setEmployee(txtNvID.getText().toString().trim());
		filter.setGroup(txtNhID.getText().toString().trim());
		filter.setItem(txtVtID.getText().toString().trim());
		filter.setPartner(partner.getPartnerID());
		filter.setStock(txtKhoID.getText().toString().trim());
		HistoryTask history = new HistoryTask();
		history.execute(filter);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		/*if(!flagFirst)
		{
			export();
			flagFirst = true;
		}*/
	}
	private class HistoryTask extends AsyncTask<ReportFilter, Void, Void> {
		ProgressDialog dialog;
		ArrayList<HistoryMaster> arrMaster;
		ArrayList<ArrayList<HistoryDetail>> arrDetail;

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (arrMaster != null && arrDetail != null) {
				HistoryAdapter historyAdapter = new HistoryAdapter(getBaseContext(), arrMaster, arrDetail);
				lstReport.setAdapter(historyAdapter);
				double total = 0;
				for (HistoryMaster hm : arrMaster) {
					total = total+hm.getT_tien();
				}
				txtTotal.setText(Formats.money(total));
			}
			dialog.dismiss();
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = new ProgressDialog(PartnerHistory.this);
			dialog.setMessage(getResources().getString(R.string.alert_waiting));
			dialog.show();
		}

		@Override
		protected Void doInBackground(ReportFilter... p) {
			// TODO Auto-generated method stub
			arrMaster = WebService.getHistoryMaster(getBaseContext(), p[0]);
			arrDetail = WebService
					.getHistoryDetail(getBaseContext(), arrMaster);
			return null;
		}
	}
	OnLongClickListener onLong = new OnLongClickListener() {

		@Override
		public boolean onLongClick(View v) {
			// TODO Auto-generated method stub
			if (v.getId() == R.id.txtNvID) {
				Intent i = new Intent(getBaseContext(), SelectEmployee.class);
				startActivityForResult(i, SelectEmployee.SELECTED_EMPLOYEE_KEY);
			} else if (v.getId() == R.id.txtKhoID) {
				Intent i = new Intent(getBaseContext(), SelectStock.class);
				startActivityForResult(i, SelectStock.SELECTED_STOCK_KEY);
			}
			return true;
		}
	};
	
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			switch (requestCode) {
			case SelectEmployee.SELECTED_EMPLOYEE_KEY:
				Employee e = (Employee) bundle
						.getSerializable(SelectEmployee.SELECTED_EMPLOYEE);
				txtNvID.setText(e.getEmployeeID());
				break;
			case SelectStock.SELECTED_STOCK_KEY:
				Stock s = (Stock) bundle
						.getSerializable(SelectStock.SELECTED_STOCK);
				txtKhoID.setText(s.getStockID());
				break;
			}
		}
	};

}
